package com.restaurant.service.impl;

import com.restaurant.bean.User;
import com.restaurant.dao.UserDao;
import com.restaurant.service.UserService;
import com.restaurant.util.MyBatisUtils;
import com.restaurant.util.Result;
import com.restaurant.util.StringUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author OUOrder
 */
public class UserServiceImpl implements UserService {
    private SqlSession sqlSession;
    private UserDao userDao;

    public UserServiceImpl() {
        this.sqlSession = MyBatisUtils.getSqlSession();
        this.userDao = sqlSession.getMapper(UserDao.class);
    }

    public Result getUser(int uid) {
        User user = userDao.selectByUid(uid);
        if(user == null){
            return Result.ok(100,"网络故障");
        }
        return Result.ok(200,user);
    }

    /**
     * 根据用户邮箱获取对应的用户对象，并把获取到的对象封装到结果类result
     * @param email
     * @param password
     * @return
     */
    @Override
    public Result getUserByEmail(String email,String password) {

        List<User> users = userDao.selectByEmail(email);
        if (users == null){
            return Result.ok(100,"邮箱不存在，请注册");
        }
        if (password != null){
            for (User user : users) {
                if (password.equals(user.getPassword()))
                    return Result.ok(Result.OK,"登录成功",user);
            }
        }
        return Result.ok(100,"密码错误");
    }

    /**
     * 根据用户手机号获取对应的用户对象，并把获取到的对象封装到结果类result
     * @param phone
     * @param password
     * @return
     */
    @Override
    public Result getUserByPhone(String phone, String password) {
        List<User> users = userDao.selectByPhone(phone);
        if (users == null){
            return Result.ok(100,"手机号不存在，请注册");
        }
        if (password != null){
            for (User user : users) {
                if (password.equals(user.getPassword()))
                    return Result.ok(Result.OK,"登录成功",user);
            }
        }
        return Result.ok(100,"密码错误");

    }

    /**
     * 根据swing传过来的user对象添加用户
     * 添加成功返回Result对象
     * @param user
     * @return 返回一个result，成功状态码为200，失败看着办
     */
    @Override
    public Result addUser(User user) {
        int users = userDao.insertUser(user);
        if (users == 0) {
            return Result.ok(100,"修改失败");
        } else {
            sqlSession.commit();
            return Result.ok(200, "成功");
        }

    }

	@Override
	public Result setUser(User user) {
		if (user == null){
		    return Result.ok(100,"请传入数据");
        }
        int i = userDao.updateUser(user);
		if (i == 0){
		    return Result.ok(100,"网络故障，修改失败");
        }
		sqlSession.commit();
        return Result.ok(200,"修改成功");
	}

    @Override
    public Result isPhoneOrEmail(String phone, String password) {
        if (StringUtil.isEmpty(phone) || StringUtil.isEmpty(password))
            return  Result.ok(100,"用户名或密码不能为空");
        int index = phone.lastIndexOf("@");
        if (index == -1 && phone.length() != 11){
            return Result.ok(100,"用户名错误");
        }
        return index != -1 ? this.getUserByEmail(phone,password) : this.getUserByPhone(phone,password);

    }

    @Override
    public Result addUser(String name,String phone, String pwd) {

        if (StringUtil.isEmpty(phone) || StringUtil.isEmpty(pwd) || StringUtil.isEmpty(name))
            return  Result.ok(100,"用户名或密码不能为空");
        int index = phone.lastIndexOf("@");
        System.out.println(index);
        if ((index == -1 && phone.length() != 11)){
            return Result.ok(100,"不是手机号和邮箱");
        }
        User user = new User();
        user.setPassword(pwd);
        user.setUsername(name);
        boolean isDigit = true;     //判断手机号是否为数字
        try {
            if (index != -1) {
                user.setEmail(phone);
                user.setPassword(pwd);
                user.setUsername(name);
                return this.addUser(user);
            }
            for(char c: phone.toCharArray()){
                if(!Character.isDigit(c)){
                    isDigit=false;
                }
            }
            if (isDigit&&phone.length() == 11){
                user.setPhoneNumber(phone);
                return this.addUser(user);
            }
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
        }
        return Result.ok(100,"网络故障，添加失败");
    }

    @Override
    public Result getBalance(String phone, double balance) {
        User user = userDao.selectByPhoneNumber(phone, balance);
        if (user == null){
            return Result.ok(100,"余额不足");
        }
        return Result.ok(200,"购买成功",user);
    }

    @Override
    public Result setUserBalance(String phone, double balance) {
        List<User> users = userDao.selectByPhone(phone);
        if (users == null || users.isEmpty()){
            return Result.ok(100,"手机号错误");
        }
        User user = users.get(0);
        user.setBalance(user.getBalance() - balance);
        int i = userDao.updateUser(user);
        if (i == 0){
            return Result.ok(100,"网络故障");
        }
        return Result.ok(Result.OK,"购买成功");
    }
}
